package com.ztgf.order.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.common.utils.DateUtils;
import com.ztgf.order.common.config.YmlConfig;
import com.ztgf.order.mapper.OrderReturnExpirationDateMapper;
import com.ztgf.order.models.bo.OrderReturnExpirationDateSaveBO;
import com.ztgf.order.models.dto.OrderReturnExpirationDateDTO;
import com.ztgf.order.models.service.IOrderReturnExpirationDateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.ztgf.common.utils.UtilConstants.*;

/*
 * @author: chenyu.wang
 * @date: 2020-08-26
 * @description:售后订单有效期服务
 */
@Slf4j
@DubboService
public class OrderReturnExpirationDateServiceImpl extends ServiceImpl<OrderReturnExpirationDateMapper, OrderReturnExpirationDateDTO> implements IOrderReturnExpirationDateService {

    @Resource
    private YmlConfig ymlConfig;

    /*
     * @description:添加倒计时，用于售后订单
     * @author: chenyu.wang
     * @date: 2020-08-26
     */
    @Override
    public boolean add(OrderReturnExpirationDateSaveBO dateDTO) {
        OrderReturnExpirationDateDTO dto = new OrderReturnExpirationDateDTO();
        dto.setOrderReturnId(dateDTO.getOrderReturnId());
        dto.setStatus(dateDTO.getStatus());
        dto.setExpirationTime(dateDTO.getExpirationTime());
        dto.setShopType(dateDTO.getShopType());
        dto.setId(ymlConfig.getPkId());
        return this.insert(dto);
    }

    /*
     * @description:更新倒计时，空就新增，否则就修改，一条售后就一条倒计时记录
     * @author: chenyu.wang
     * @date: 2020-08-27
     */
    @Override
    public boolean addCheck(OrderReturnExpirationDateSaveBO bo) {
        Wrapper<OrderReturnExpirationDateDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("order_return_id", bo.getOrderReturnId());
        OrderReturnExpirationDateDTO dateDTO = this.selectOne(wrapper);
        if (null == dateDTO) {
            return this.add(bo);
        } else {
            OrderReturnExpirationDateDTO update = new OrderReturnExpirationDateDTO();
            update.setOrderReturnId(bo.getOrderReturnId());
            update.setStatus(bo.getStatus());
            update.setExpirationTime(bo.getExpirationTime());
            update.setShopType(bo.getShopType());
            update.setId(dateDTO.getId());
            return this.updateById(update);
        }

    }

    /*
     * @description:卖家延长发货倒计时,售后订单id
     * @author: chenyu.wang
     * @date: 2020-09-02
     */
    @Override
    public boolean extendOrderTime(Long id, Integer time) {
        log.info("售后延长发货，售后订单id : {}", id);
        Wrapper<OrderReturnExpirationDateDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("order_return_id", id);
        wrapper.andNew().eq("status", TWO).or().eq("status", ONE);
        List<OrderReturnExpirationDateDTO> list = this.selectList(wrapper);
        List<OrderReturnExpirationDateDTO> realList = new ArrayList<>();
        if (isNull(list)) {
            log.info("暂未查询到售后订单倒计时!");
            return false;
        }
        for (OrderReturnExpirationDateDTO expirationDateDTO : list) {
            if (expirationDateDTO == null || expirationDateDTO.getIsExtend() == ONE) {
                log.info("售后订单倒计时为空! : {}", expirationDateDTO);
                return false;
            }
            Date currentDateTime = expirationDateDTO.getExpirationTime();//原有时间
            Date expireTime = DateUtils.addDay(currentDateTime, time);//过期时间
            OrderReturnExpirationDateDTO updateDTO = new OrderReturnExpirationDateDTO();
            updateDTO.setId(expirationDateDTO.getId());
            updateDTO.setExpirationTime(expireTime);
            updateDTO.setIsExtend(ONE);
            realList.add(updateDTO);
        }
        return this.updateBatchById(realList);
    }

    @Override
    public List<OrderReturnExpirationDateDTO> queryAllExpirationAfterOrder(Long lastNo) {
        //页码
        Page<OrderReturnExpirationDateDTO> page = new Page<>(0, 50);
        //当前时间
        String currentTime = DateUtils.dateToStr(DateUtils.getCurrentDateTime(), DateUtils.DATE_FORMAT_DATETIME);
        Wrapper<OrderReturnExpirationDateDTO> wrapper = new EntityWrapper<>();
        wrapper.le("expiration_time", currentTime);
        wrapper.in("status", Arrays.asList("3", "4"));
        wrapper.orderBy("order_return_id", true);
        wrapper.gt("order_return_id", lastNo);
        return baseMapper.selectPage(page, wrapper);
    }
}
